package cn.net.yto.school.service.impl;

import cn.net.yto.base.dao.pagination.PaginationRequest;
import cn.net.yto.base.dao.pagination.PaginationResponse;
import cn.net.yto.school.dao.entities.TbCourse;
import cn.net.yto.school.dao.entities.TbFile;
import cn.net.yto.school.dao.repositories.TbCourseRepository;
import cn.net.yto.school.dao.repositories.TbFileRepository;
import cn.net.yto.school.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private TbCourseRepository courseRepository;

    @Autowired
    private TbFileRepository fileRepository;

    @Override
    public PaginationResponse<TbCourse> findResultsByPagination(PaginationRequest paginationRequest) {
        return this.courseRepository.findResultsByPagination(paginationRequest);
    }

    @Override
    @Transactional
    public void deleteCourseById(Integer id) {
        this.courseRepository.delete(id);
    }

    @Override
    @Transactional
    public TbCourse findOne(Integer id) {
        return this.courseRepository.findOne(id);
    }

    @Override
    public List<TbFile> findResultsByCourseId(Integer courseId) {
        return this.fileRepository.findAllBySql(TbFile.class, "select * from tb_file where file_belong_to = " + courseId);
    }

    @Override
    @Transactional
    public void saveCourse(TbCourse course) {
    }

    @Override
    public List<TbCourse> findAll() {
        return this.courseRepository.findAll();
    }
}
